package com.gitee.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 读取配置文件工具类
 *  
 * @author  赵炎
 * @version  [V1.00, 2018年9月7日]
 * @see  [相关类/方法]
 * @since V1.00
 */
public class PropertiesUtil
{
    private static final Logger LOG = LoggerFactory.getLogger(PropertiesUtil.class);
    
    /**
     * commonMap 公共参数
     */
    private static Map<String, Object> commonMap = new HashMap<String, Object>();
    
    /** 
     * 加载配置文件
     * @param fileName 文件名
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static void loadProperties(String fileName)
    {
      
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
        Properties prop = new Properties();
        try
        {
            prop.load(is);
            
            Set<Object> set = prop.keySet();
            Iterator<Object> it = set.iterator();
            while (it.hasNext()) 
            {
                Object key = it.next();
                commonMap.put(key + "", prop.get(key));
            }
        }
        catch (IOException e)
        {
            LOG.error(" load file error file : {} \n e:", fileName, e);
        }
        finally
        {
            if (is != null) 
            {
                try
                {
                    is.close();
                }
                catch (IOException e)
                {
                    LOG.error(" close io exception ", e);
                }
            }
        }
    }
    
    /** 
     * 获取值
     * @param key
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String getStrValue(String key) 
    {
       return  (String)(commonMap.get(key) != null ? commonMap.get(key) : "");
    }
    /** 
     * 获取值
     * @param key
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static int getIntValue(String key) 
    {
       return (int)commonMap.get(key);
    }
    /** 
     * 获取值
     * @param key
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static double getDoubleValue(String key) 
    {
       return (double)commonMap.get(key);
    }
    
    /** 
     * 获取值
     * @param key
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static long getLongValue(String key) 
    {
       return (long)commonMap.get(key);
    }
    
}
